
import Fly from 'flyio/dist/npm/wx'
const fly = new Fly()
// const host = 'http://192.168.2.140:8080/'
// fly.config.baseURL = host
fly.config.timeout = 5 * 1000
// 添加请求拦截器
fly.interceptors.request.use((request) => {
    // wx.showLoading({
    //   title: '拼命加载中',
    //   mask: true
    // })
    // console.log(request)
    // request.headers["X-Tag"] = "flyio";
    // request.headers['content-type']= 'application/json';
    if(!wx.getStorageSync('token')) return
    request.headers = {
      'content-type': 'application/json',
      'Authorization': wx.getStorageSync('token')
    }
   
    let authParams = {
      // 公共参数
    //   'categoryType': 'SaleGoodsType@sim',
    //   'streamNo': 'wxapp153570682909641893',
    //   'reqSource': 'MALL_H5',
    //   'appid': 'string',
    //   'timestamp': new Date().getTime(),
    //   'sign': 'string'
    }
    request.body && Object.keys(request.body).forEach((val) => {
      if (request.body[val] === '') {
        delete request.body[val]
      };
    })
    request.body = {
      ...request.body,
      ...authParams
    }
    return request
  })
// 添加响应拦截器
fly.interceptors.response.use(
    (response) => {
      // wx.hideLoading()
      if(response.data.code==200){
        wx.hideToast()
        return response.data
      }else if(response.data.code==201){
        wx.hideToast()
        return response.data // 请求成功之后将返回值返回
      }else{
        wx.showToast({
          title: response.data.msg,
          icon:'none',
          duration: 2000
        })
      }
      
    },
    (err) => {
      // 请求出错，根据返回状态码判断出错原因
      console.log(err)
      wx.hideLoading()
      if (err.status == 500) {
        wx.showToast({
          title: "网络请求失败",
          icon:'none',
          duration: 2000
        })
        return "网络连接异常"
      } else if (err.status == 404) {
        wx.showToast({
          title: "网络请求失败",
          icon:'none',
          duration: 2000
        })
        return "服务器错误"
      } else if (err.status == 401) {
        return "密码不正确"
      } else {
        if (err.response) {
          return err.response
        } else {
          return '请求数据失败,请稍后再试'
        }
      };
    }
  )
export default fly